package pers.dzk.myplayer;

import javax.sound.sampled.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.IOException;

/**
 * @author 丁泽凯
 */
public class Player extends JFrame {
    private boolean isStart = false;
    private int i = 0;
    private String[][] strings;
    private Clip bgm;
    private static AudioInputStream ais;
    private String name;
    private int speed;
    private int time;
    private int time2;
    public Player(String name) {
        this.strings = Factory.getString(new File("txt/"+name));
        this.name = name;
        this.speed = Factory.getFrameRate(new File("video/"+name+".mp4"))+2;
        time = strings.length/speed+4;
        time2 = time;
        music();
        timeInit();
        addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
            }

            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode() == KeyEvent.VK_SPACE){
                    isStart = !isStart;
                    if(bgm.isRunning()){
                        bgm.stop();
                    }else {
                        bgm.start();
                    }
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {

            }
        });
        setSize(1200, 1200);
        setLocationRelativeTo(null);
        setVisible(true);
        setDefaultCloseOperation(3);
        add(new MyPanel());
        open();
    }
    public void timeInit(){
        Thread t = new Thread(() -> {
            while (true) {
                System.out.println();
                while (time2 > 0 && isStart) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    time2--;
                }
            }
        });
        t.start();
    }
    public void music() {
        try {
            bgm = AudioSystem.getClip();
            ais = AudioSystem.getAudioInputStream(new File("music/"+name+".wav"));
            bgm.open(ais);
        } catch (LineUnavailableException | UnsupportedAudioFileException | IOException e) {
            e.printStackTrace();
        }
    }

    public void open() {
        Thread t = new Thread(() -> {
            while (true) {
                System.out.println();
                while (isStart) {
                    repaint();
                    try {
                        Thread.sleep(speed);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }

        });
        t.start();
    }

    class MyPanel extends JPanel {
        public MyPanel() {
            setDoubleBuffered(true);
            setBackground(Color.black);
        }

        @Override
        public void paint(Graphics g) {
            super.paint(g);
            Graphics2D g2d = (Graphics2D) g;
            if (i < strings.length && isStart) {
                i++;
            }
            g2d.setFont(new Font("仿宋", Font.PLAIN, 31));
            for (int j = 0; j < strings[i][j].length(); j++) {
                char[] txt = strings[i][j].toCharArray();
                for (int z = 0; z < txt.length; z++) {
                    if (txt[z] == '%') {
//                        g2d.setColor(new Color(251,96,127,255));
                        g2d.setColor(new Color(255,255,255,255));
                    } else if (txt[z] == '*') {
                        g2d.setColor(new Color(255,85,163,255));
                    } else if (txt[z] == 'o') {
                        g2d.setColor(new Color(195,187,255,255));
                    } else if (txt[z] == '!') {
                        g2d.setColor(new Color(209,159,232,255));
                    } else if (txt[z] == ';') {
                        g2d.setColor(new Color(8,232,222,255));
                    } else {
                        g2d.setColor(new Color(255,0,127,255));
                    }
                    g2d.drawString(txt[z] + "", 16 * z, 34 * j);
                }
            }
            //绘制进度条
            int boxWidth = getWidth()/time;
            for (int i = 0; i < time - time2; i++) {
                g2d.setColor(new Color(255,255,255,255));
                g2d.fillRect(i*boxWidth,34*(strings[0].length-1)-30,boxWidth-7,40);
            }
            //绘制时间
            g2d.setColor(new Color(0,128,0,255));
            g2d.drawString(time2/60+":"+time2%60+" / "+time/60+":"+time%60,strings[0][0].length()*16-215,34*(strings[0].length-1)-10);
        }

    }
}
